/**
 * @file TEST_CO_pt.c
 * @author Linquan 
 * @brief 单线程进行多任务调度。
 * 这里创建了两个任务，分别等待各自的计数器累计到指定值后才能继续往下执行.
 * 效果是两个任务交替执行。
 * @version 0.1
 * @date 2025-10-10
 * 
 * @copyright Copyright (c) 2025
 * 
 */

#include "app_config.h"
#if TEST_CO_PT

/* 由于协程仅有宏组成，所以与task共用一个头文件 */
#include "tasks.h"
#include "sys_tick.h"


uint16_t count_1 = 0;
uint16_t count_2 = 0;

/* 任务1 */
void CO_task_1()
{
     CO_INIT(wait_reply);
     CO_START(wait_reply);
     while(1)
     {
       
        printf("CO task1-1\r\n");

        CO_WAIT_WHILE(wait_reply, (count_1 < 100));
 
        count_1 = 0;

        printf("CO task1-1111111\r\n");

        CO_YIELD(wait_reply)
     }
     // 执行结束
     CO_END(wait_reply);
}

/* 任务2 */
void CO_task_2()
{
     CO_INIT(wait_reply);
     CO_START(wait_reply);
     while(1)
     {
       
        printf("CO task2-1\r\n");

        CO_WAIT_WHILE(wait_reply, (count_2 < 200));
 
        count_2 = 0;

        printf("CO task2-222222222\r\n");

        CO_YIELD(wait_reply)
     }
     // 执行结束
     CO_END(wait_reply);
}





/**
 * @brief 时间戳回调函数
 * 
 */
static void Timer_callbak()
{   
    count_1++;
    count_2++;
    if(count_1 %10 == 0) printf("time = %d \r\n", count_1);
}
    
/**
 * @brief 调度框架时基初始化
 * 
 * @param ptr 
 */
void  TImer_Init(void *ptr)
{ 
    sys_timer_callback_add(&Timer_callbak); // 赋值 定时器回调函数
}
EXPORT_Lv2_PREV(TImer_Init, NULL);



void CO_schedul(void * ptr)
{
    CO_task_1();
    CO_task_2();
}

EXPORT_Lv7_APP_MANGE(CO_schedul,NULL);




#endif



